package com.bocai.kelala.ui.travel.set.act;

import android.content.Intent;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.ui.travel.main.act.MainActivity;
import com.bocai.kelala.ui.travel.set.contract.ChangePhoneContract;
import com.bocai.kelala.ui.travel.set.module.ChangePhoneModule;
import com.bocai.kelala.ui.travel.set.presenter.ChangePhonePresenter;
import com.bocai.kelala.util.SP;
import com.bocai.mybase.bean.ChangePhoneBean;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/13.
 */

public class ChangePhoneTwoActivity extends BaseActivity implements ChangePhoneContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @Inject
    ChangePhonePresenter phonePresenter;
    @BindView(R.id.btn_code)
    AppCompatButton btnCode;
    @BindView(R.id.btn_next)
    AppCompatButton btnNext;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_change_phone_two;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new ChangePhoneModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("");
        initBack(imgBack,llBack);
        edPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (edPhone.getText().toString().length() == 11&&edCode.getText().toString().length()>=4){
                    btnNext.setEnabled(true);
                    btnNext.setTextColor(getResources().getColor(R.color.color_white));
                }else{
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edPhone.getText().toString().length() == 11&&edCode.getText().toString().length()>=4){
                    btnNext.setEnabled(true);
                    btnNext.setTextColor(getResources().getColor(R.color.color_white));
                }else{
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @OnClick({R.id.btn_code,R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                if (TextUtils.isEmpty(edPhone.getText().toString())){
                    showErrorTip("请输入手机号");
                }else if (edPhone.getText().toString().length()>11){
                    showErrorTip("请输入11位有效的手机号");
                }else {
                    phonePresenter.sendCode(edPhone.getText().toString(), "2");
                }
                break;
            case R.id.btn_next:
                ChangePhoneBean bean = new ChangePhoneBean();
                bean.setPhoneNumber(edPhone.getText().toString());
                bean.setSmsVerifyCode(edCode.getText().toString());
                Log.e("bean",SP.get(App.getAppContext(),"DynamicToken","").toString());
                phonePresenter.changePhone(SP.get(this,"id","").toString(),bean);
                break;

        }
    }

    @Override
    public void getCode(String code) {
        phonePresenter.setCode();
    }

    @Override
    public void changePhoneSuccess() {
        SP.put(this,"PhoneNumber",edPhone.getText().toString());
        SP.put(this,"PhoneNumber",edPhone.getText().toString());
        showShortToast("修改成功");

        startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
        finish();
    }

    @Override
    public void setBtnCode(int index) {
        if (index == 0) {
            btnCode.setClickable(true);
            btnCode.setEnabled(true);
            btnCode.setText(getResources().getText(R.string.get_code));
        } else {
            btnCode.setClickable(false);
            btnCode.setEnabled(false);
            btnCode.setText(index + "秒");
        }
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        if (msg.equals("用户授权失败")){
            msg = "您的登录已失效，请重新登录";
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
        showShortToast(msg);
    }
}
